﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using HttpClientStudy.Config;


using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace HttpClientStudy.UnitTest.ConfigTest
{
    public class WebApiConfigTest
    {
        [Fact]
        public void WebApiOption_Test()
        {
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.AddWebApiConfiguration();

            IConfiguration configuration = configurationBuilder.Build();

            var services = new ServiceCollection();
            services.AddSingleton(configuration);
            services.UseWebApiOptions();

            var provider = services.BuildServiceProvider();
            IOptions<WebApiConfig>? webApiOptions = provider.GetService<IOptions<WebApiConfig>>();

            var webApiConfig = webApiOptions?.Value;

            Assert.NotNull(webApiOptions);
            Assert.NotNull(webApiConfig);
            Assert.NotEmpty(webApiConfig.BaseUrl);
            Assert.NotEmpty(webApiConfig.WebAppMutexName);
        }

        [Fact]
        public void WebApiOption_Test2()
        {
            IOptionsMonitor<WebApiConfig> webApiMonitor = WebApiConfigManager.GetWebApiConfigOption();

            Assert.NotNull(webApiMonitor);

            var webApiConfig = webApiMonitor.CurrentValue;

            Assert.NotNull(webApiConfig);
            Assert.NotEmpty(webApiConfig.WebAppMutexName);
        }

        [Fact]
        public void DNS_Test()
        {

            var ips = Dns.GetHostAddresses(WebApiConfigManager.GetWebApiConfig().Host);

            Assert.NotEmpty(ips);
        }
    }
}
